//
//  BaseHttpClient.m
//  NetworkingSevers
//
//  Created by WorkSpace on 4/1/14.
//
//

#import "BaseHttpClient.h"


@implementation BaseHttpClient

//@synthesize delegate;

+ (BaseHttpClient*)sharedBaseHttpClientUrl:(NSString*)url
{
    NSString *urlStr = url ;
    static dispatch_once_t pred;
    static BaseHttpClient *_sharedBaseHttpClient = nil;
    dispatch_once(&pred, ^{ _sharedBaseHttpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:urlStr]]; });
    return _sharedBaseHttpClient;
}
-(void)postDataWithPath:(NSString *)path  withAction:(NSString *)actionName WithParameters:(NSDictionary *)parameters block:(void(^)(id json))processResult failblock:(void(^)(NSError *error))failresult
{
    [ProgressHUD show:@"努力请求数据中"];
    [self postPath:path
        parameters:parameters
           success:^(AFHTTPRequestOperation *operation, id responseObject)
           {
               processResult(responseObject);
               [ProgressHUD dismiss];
           }
           failure:^(AFHTTPRequestOperation *operation, NSError *error)
           {
               failresult(error);
               [ProgressHUD dismiss];
           }];
}
//获取当前UIViewController
-(UIViewController *)getCurrentRootViewController {
    
    UIViewController *result;
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows)
        {
            if (topWindow.windowLevel == UIWindowLevelNormal)
            {
                break;
            }
        }
    }
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    id nextResponder = [rootView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]])
    {
        result = nextResponder;
    }
    else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
    {
        result = topWindow.rootViewController;
    }
    else
    {
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    }
    return result;
}
@end
